package testcase;

import game.Game;
import io.ConsoleIO;
import io.IOManager; 
import player.Player;
import validation.*;

import java.io.*;
import java.util.NoSuchElementException;
import card.Card;
import card.Cardset;
import junit.framework.TestCase;






public class GameTest extends TestCase {
	
	class IOStub extends ConsoleIO{
		public String lastPrint;
		
		@Override
		public void println() {					
		}

		@Override
		public void println(String s) {
			lastPrint = s;			
		}

		@Override
		public void print(String s) {
			lastPrint = s;			
		}

		@Override
		public void print(Card c, int index) {						
		}

		@Override
		public void print(Cardset cs) {						
		}		
	}
		
	public void setUp() {
		Type type;
		type = Nuke.getInstance();
		type.clear();
		type = Bomb.getInstance();
		type.clear();
		type = Solo.getInstance();
		type.clear();
		type = Pair.getInstance();
		type.clear();
		type = Trio.getInstance();
		type.clear();
		type = SoloChain.getInstance();
		type.clear();
		type = PairChain.getInstance();
		type.clear();
		type = TrioChain.getInstance();
		type.clear();
		type = TrioWithSoloChain.getInstance();
		type.clear();
		type = TrioWithPairChain.getInstance();
		type.clear();
		type = TrioWithSolo.getInstance();
		type.clear();
		type = TrioWithPair.getInstance();
		type.clear();
		type = FourWithDualSolo.getInstance();
		type.clear();
		type = FourWithDualPair.getInstance();
		type.clear();
		
	}
	
	public void tearDown() {
		
	}
	
	public void testNoLastWinnerGame(){
		String input = "1 3 ";
		for (int i=0;i<19;i++)
		{
			input += "0 t c t c t c ";
		}
		
		input += "0 t c n";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		
		IOStub ioStub = new IOStub();		
		IOManager ioManager = new IOManager(ioStub);
		Player players[] = new Player[3];
		
		players[0] = new Player("A",0,ioManager);
		players[1] = new Player("B",1,ioManager);
		players[2] = new Player("C",2,ioManager);
		
			
		
		Game game = new Game(players,ioManager);
		
		boolean bGameLongerThanExpected = false;
		
		try {			
			game.start();
		} catch (NoSuchElementException e) {
			bGameLongerThanExpected = true;
		}
						
		assertEquals(false, bGameLongerThanExpected);
		assertEquals("Game End!", ioStub.lastPrint);
		assertEquals(game.getWinner(),players[1]);
	}
	
	public void testLastWinnerGame(){
		String input = "1 3 ";
		for (int i=0;i<19;i++)
		{
			input += "0 t c t c t c ";
		}
		
		input += "0 t c n";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		
		IOStub ioStub = new IOStub();
		IOManager ioManager = new IOManager(ioStub);
		
		Player players[] = new Player[3];
		
		players[0] = new Player("A",0,ioManager);
		players[1] = new Player("B",1,ioManager);
		players[2] = new Player("C",2,ioManager);
		
			
		
		Game game = new Game(players,ioManager,players[1]);
		
		boolean bGameLongerThanExpected = false;
		
		try {			
			game.start();
		} catch (NoSuchElementException e) {
			bGameLongerThanExpected = true;
		}
			
		
		assertEquals(false, bGameLongerThanExpected);
		assertEquals("Game End!", ioStub.lastPrint);
		assertEquals(game.getWinner(),players[2]);
	}
	
	
	public void testLandlord(){
		String input = "3 ";
		for (int i=0;i<19;i++)
		{
			input += "0 t c t c t c ";
		}
		
		
		input += "0 t c n";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		
		IOStub ioStub = new IOStub();
		IOManager ioManager = new IOManager(ioStub);
		
		Player players[] = new Player[3];
		
		players[0] = new Player("A",0,ioManager);
		players[1] = new Player("B",1,ioManager);
		players[2] = new Player("C",2,ioManager);
		
			
		
		Game game = new Game(players,ioManager);
		
		boolean bGameLongerThanExpected = false;
		
		try {			
			game.start();
		} catch (NoSuchElementException e) {
			bGameLongerThanExpected = true;
		}
		
		assertEquals(false, bGameLongerThanExpected);
		assertEquals("Game End!", ioStub.lastPrint);
		assertEquals(game.getWinner(),players[0]);
	}
	

}
